package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.dao.cargo.ShippingOrderDao;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.domain.cargo.ShippingOrder;
import cn.itcast.domain.cargo.ShippingOrderExample;
import cn.itcast.service.cargo.ShippingOrderService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ShippingOrderServiceImpl implements ShippingOrderService {

    @Autowired
    private ShippingOrderDao shippingOrderDao;

    @Autowired
    private PackingDao packingDao;

    //分页查询所有委托单*
    public PageInfo findAll(ShippingOrderExample shippingOrderExample,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ShippingOrder> shippingOrders = shippingOrderDao.selectByExample(shippingOrderExample);
        PageInfo pageInfo = new PageInfo(shippingOrders);
        return pageInfo;
    }

    //通过id查询委托单
    public ShippingOrder findById(String id) {
        return shippingOrderDao.selectByPrimaryKey(id);
    }

    //添加委托单
    public void save(ShippingOrder shippingOrder) {
        Packing packing = packingDao.selectByPrimaryKey(shippingOrder.getShippingOrderId());
        //已装船
        packing.setState(2l);
        //更新装箱数据
        packingDao.updateByPrimaryKeySelective(packing);
        //设置信用证号
        shippingOrder.setLcNo(UUID.randomUUID().toString().replace("-",""));
        //设置委托单的状态
        shippingOrder.setState(0);
       /* //设置创建人
        shippingOrder.setCreateBy(packing.getCreateBy());
        //设置创建部门
        shippingOrder.setCreateDept(packing.getCreateDept());*/
       //设置创建时间
        shippingOrder.setCreateTime(new Date());
        shippingOrderDao.insertSelective(shippingOrder);
    }

    //编辑委托单
    public void update(ShippingOrder shippingOrder) {
        shippingOrderDao.updateByPrimaryKeySelective(shippingOrder);
    }

    //删除委托单
    public void delete(String id) {
        shippingOrderDao.deleteByPrimaryKey(id);
    }
}
